package com.kanshu.client.ui.activity.ipc;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.kanshu.basictest.BookManager;
import com.kanshu.basictest.IBookShelfChangedListener;
import com.kanshu.basictest.model.ipc.Book;
import com.kanshu.client.R;
import com.kanshu.client.model.User;
import com.kanshu.client.model.menu.MenuItem;
import com.kanshu.client.ui.base.BaseMenuActivity;

import java.util.ArrayList;
import java.util.List;

import static com.kanshu.client.constants.PubContants.MSG_NEW_BOOK_ADDED;

public class ContentProviderTestActivity extends BaseMenuActivity {

    private static final String TAG = "ProviderTestActivity";

    public static final String AUTHORITY = "com.kanshu.basictest.book.provider";

    public static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/book");
    public static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/user");

    @Override
    protected String provideBarTitle() {
        return "ContentProvider测试";
    }

    @Override
    protected List<? extends MenuItem> provideMenuData() {
        List<MenuItem> menuItems = new ArrayList<>();
        menuItems.add(0, new MenuItem("查询图书列表", R.color.blue_200));
        menuItems.add(1, new MenuItem("查询用户列表", R.color.blue_200));
        return menuItems;
    }


    @Override
    protected void init() {
        super.init();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0://
                queryAllBook();
                break;
            case 1://
                queryAllUser();
                break;
            case 2://
                break;
            default:// do nothing
                break;

        }

    }

    private StringBuilder convertBook(Cursor cursor) {
        StringBuilder msg = new StringBuilder();
        if (cursor == null) {
            return msg.append("没有查询到数据！");
        }
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            Book book = new Book(String.valueOf(id), name);
            msg.append(book.toString()).append("\n");
        }
        cursor.close();
        return msg;
    }

    private StringBuilder convertUser(Cursor cursor) {
        StringBuilder msg = new StringBuilder();
        if (cursor == null) {
            return msg.append("没有查询到数据！");
        }
        while (cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            int sex = cursor.getInt(2);
            User user = new User(id, name, sex);
            msg.append(user.toString()).append("\n");
        }
        cursor.close();
        return msg;
    }

    private void queryAllBook() {
        Cursor cursor = getContentResolver().query(BOOK_CONTENT_URI, new String[]{"_id", "name"}, null, null, null);
        StringBuilder books = convertBook(cursor);
        showPromptDialog("查询图书列表", books.toString());
    }

    private void queryAllUser() {
        Cursor cursor = getContentResolver().query(USER_CONTENT_URI, new String[]{"_id", "name", "sex"}, null, null, null);
        StringBuilder users = convertBook(cursor);
        showPromptDialog("查询图书列表", users.toString());
    }

}
